(function(Jml, EFactory) {
	/**
	 * array
	 */
	EFactory.addObject("array", {

		_buildObject: function(op) {
			var content = [];
			var columns = this.domTemplate.children();
			if (columns.length) {
				var columnConfig = new Ccay.Jml.ConfigLoader(null, this.page);
				$(columns).each(function() {
					columnConfig.load($(this));
					content.push('{' + columnConfig.option + '}');
				});
			}
			return this.page.buildFn("return [" + content + "];",null, true);
		}
	});
	/**
	 * service
	 */
	EFactory.addObject("service", {
		_buildObject: function(op) {
			//要返回的对象
			var serviceData = {};
			//子元素
			var columns = this.domTemplate.children();

			if (columns.length) {
				serviceData = this._haveChildren(columns, op);
			} else {
				serviceData = this._noChildren(op);
			}
			return serviceData;
		},

		/* 有子元素方法 */
		_haveChildren: function(columns, op) {
			var data = {}, _obj = this,
				cOption,
				columnConfig = new Ccay.Jml.ConfigLoader(null, _obj.page);

			columns.each(function() {
				columnConfig.load($(this)).execute();
				cOption = columnConfig.option;
				var serviceType = cOption.serviceType;

				if (serviceType == 'ajax') {
					var text = _obj.page.buildFn("{" + $(this).text() + "}", true,true);
					// 加上回调函数
					$.extend(cOption, text);
					if (cOption.name) {
						// 这里不用像下面data那样加上op.name，因为data返回后会被setObject一次，set的时候就是set到op.name里
						data[cOption.name] = cOption;
					}
				} else if (serviceType == 'data') {
					cOption.name = op.name + "." + cOption.name;
					_obj._ajaxGetData(cOption);
				}
			});
			// _ajaxGetData里的Jml.activePage.setObject已经将service返回的数据set到了page里了，无需在set
			return data;
		},

		/* 无子元素方法 */
		_noChildren: function(op) {
			var text = this.domTemplate.text(),config;
			if(text)
			{
				if(config = this.page.buildFn("{" + text + "}", true,true))
					$.extend(op, config);
			}
			op.url && (op.url = op.url.formatByMap(this,function(item){
				return item.indexOf('page.') > -1;
			},"url"));

			return op.serviceType == 'data' ? this._ajaxGetData(op) : op;
		},

		/* 同步查询结果 */
		_ajaxGetData: function(op) {
			var page = this.page;
			op.success = $.mergeFn(op.success, function(result) {
				page.setObject(this, result);
			})
			Ccay.Core.ajax(op);
		}
	});

	//命名空间元素，会将自己的内容(内容必须为对象)，注册到全局module中
	EFactory.add("namespace", {
		_build: function(op) {
			var op, obj, content;
			this.config.execute();
			op = this.config.option;
			if (op && op.name) {
				content = this.domTemplate.text();
				if (content.length) {
					obj = this.page.buildFn("{" + content + "}", true,true);
					//注册到模块管理中
					obj && Ccay.Core.Namespace.regModule(op.name, obj);
				}
			}
		}
	});

	//页面脚本元素
	EFactory.add("script", {
		_build: function(op) {
			var content = this.domTemplate.text();
			content.length && this.page.buildFn(content,false,true);
		}
	});

	//事件元素
	EFactory.add("events", {
		_build: function(op) {
			var content = this.domTemplate.text(),elemEvents;
			if(content.length)
			{
				elemEvents = this.page.buildFn(content, true,true);
				elemEvents && this.page.fnLoad.add(function() {
					bindEvents(this,elemEvents);
				});
			}
			
		}
	});

	function bindEvents(page,elemEvents){
		var target;
		$.each(elemEvents,function(targetId,events){
			target = page.find('#' + targetId);
			if(target.length){
				$.each(events,function(eventName,fn){
					target.bind(eventName,fn);
				})
			}
		});
	}
})(Ccay.Jml, Ccay.Jml.ElementFactory);